package com.rh.bmw4s.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.rh.bmw4s.R;
import com.rh.bmw4s.fragment.FiveFragment;
import com.rh.bmw4s.fragment.FourFragment;
import com.rh.bmw4s.fragment.OneFragment;
import com.rh.bmw4s.fragment.ThreeFragment;
import com.rh.bmw4s.fragment.TwoFragment;
import com.rh.bmw4s.utills.ToastUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {
    @BindView(R.id.bottom_nav_container)
    LinearLayout bottom_nav_container;
    @BindView(R.id.fragmentRoot)
    FrameLayout fragmentRoot;
    @BindView(R.id.ll_one)
    LinearLayout llOne;
    @BindView(R.id.ll_two)
    LinearLayout llTwo;
    @BindView(R.id.ll_three)
    LinearLayout llThree;
    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.ll_four)
    LinearLayout llFour;
    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.ll_five)
    LinearLayout llFive;
    @BindView(R.id.view_1)
    View view1;
    @BindView(R.id.ll_light)
    LinearLayout llLight;
    private ArrayList<View> bottomNavs;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private Fragment oneFragment, twoFragment, threeFragment, fourFragment, fiveFragment;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        setChoose(1);
    }

    @OnClick({R.id.ll_one, R.id.ll_two, R.id.ll_three, R.id.ll_four, R.id.ll_five})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_one:
                setChoose(1);
                break;
            case R.id.ll_two:
                setChoose(2);
                break;
            case R.id.ll_three:
                setChoose(3);
                break;
            case R.id.ll_four:
                setChoose(4);
                break;
            case R.id.ll_five:
                setChoose(5);
                break;
        }
    }

    public void setChoose(int i) {
        llOne.setSelected(false);
        llTwo.setSelected(false);
        llThree.setSelected(false);
        llFour.setSelected(false);
        llFive.setSelected(false);
        switch (i) {
            case 1:
                llLight.setBackgroundResource(R.mipmap.light_01);
                llOne.setSelected(true);
                startFragment(1);
                break;
            case 2:
                llLight.setBackgroundResource(R.mipmap.light_02);
                llTwo.setSelected(true);
                startFragment(2);
                break;
            case 3:
                llLight.setBackgroundResource(R.mipmap.light_03);
                llThree.setSelected(true);
                startFragment(3);
                break;
            case 4:
                llLight.setBackgroundResource(R.mipmap.light_04);
                llFour.setSelected(true);
                startFragment(4);
                break;
            case 5:
                llLight.setBackgroundResource(R.mipmap.light_05);
                llFive.setSelected(true);
                startFragment(5);
                break;
        }
    }

    private void startFragment(int i) {
        transaction = manager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (i) {
            case 1:
                if (oneFragment != null) {
                    transaction.show(oneFragment);
                } else {
                    oneFragment = new OneFragment();
                    transaction.add(R.id.fragmentRoot, oneFragment, "one");
                }
                break;
            case 2:
                if (twoFragment != null) {
                    transaction.show(twoFragment);
                } else {
                    twoFragment = new TwoFragment();
                    transaction.add(R.id.fragmentRoot, twoFragment, "two");
                }
                break;
            case 3:
                if (threeFragment != null) {
                    transaction.show(threeFragment);
                } else {
                    threeFragment = new ThreeFragment();
                    transaction.add(R.id.fragmentRoot, threeFragment, "three");
                }
                break;
            case 4:
                if (fourFragment != null) {
                    transaction.show(fourFragment);
                } else {
                    fourFragment = new FourFragment();
                    transaction.add(R.id.fragmentRoot, fourFragment, "four");
                }
                break;
            case 5:
                if (fiveFragment != null) {
                    transaction.show(fiveFragment);
                } else {
                    fiveFragment = new FiveFragment();
                    transaction.add(R.id.fragmentRoot, fiveFragment, "five");
                }
                break;
        }
        transaction.commitAllowingStateLoss();
    }

    /**
     * 隐藏fragment
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (oneFragment != null) {
            transaction.hide(oneFragment);
        }
        if (twoFragment != null) {
            transaction.hide(twoFragment);
        }
        if (threeFragment != null) {
            transaction.hide(threeFragment);
        }
        if (fourFragment != null) {
            transaction.hide(fourFragment);
        }
        if (fiveFragment != null) {
            transaction.hide(fiveFragment);
        }
    }


    long exitTime = 0;

    // 点击返回按钮
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ToastUtils.toastForShort(MainActivity.this, "再按一次退出程序");
                exitTime = System.currentTimeMillis();
                return false;
            } else {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

}
